#!/bin/bash
# Detailed quality comparison between enriginenc and topsenc

set -e

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

export GST_PLUGIN_PATH=$(pwd)/build:$GST_PLUGIN_PATH

echo "=== Detailed Quality Comparison ==="
echo ""

# Test parameters
WIDTH=1280
HEIGHT=720
FRAMES=300
BITRATE=2000

echo -e "${YELLOW}Test Configuration:${NC}"
echo "  Resolution: ${WIDTH}x${HEIGHT}"
echo "  Frames: ${FRAMES}"
echo "  Bitrate: ${BITRATE} kbps"
echo "  Pattern: SMPTE color bars"
echo ""

# Test 1: enriginenc with detailed logging
echo -e "${YELLOW}[1/4] Encoding with enriginenc...${NC}"
GST_DEBUG=enriginenc:3 gst-launch-1.0 -q videotestsrc pattern=smpte num-buffers=$FRAMES ! \
  video/x-raw,format=NV12,width=$WIDTH,height=$HEIGHT,framerate=30/1 ! \
  enriginenc codec=h264 rc-mode=cbr bitrate=$BITRATE profile=high ! \
  h264parse ! filesink location=compare_enrigin.h264 2>&1 | grep -i "bitrate\|profile\|level" || true

if [ -f compare_enrigin.h264 ]; then
    ENRIGIN_SIZE=$(stat -c%s compare_enrigin.h264)
    echo -e "${GREEN}✓ enriginenc: ${ENRIGIN_SIZE} bytes${NC}"
fi

# Test 2: topsenc with detailed logging
echo ""
echo -e "${YELLOW}[2/4] Encoding with topsenc...${NC}"
gst-launch-1.0 -q videotestsrc pattern=smpte num-buffers=$FRAMES ! \
  video/x-raw,format=NV12,width=$WIDTH,height=$HEIGHT,framerate=30/1 ! \
  topsenc codec=h264 rc-mode=cbr bitrate=$BITRATE profile=high ! \
  h264parse ! filesink location=compare_topsenc.h264 2>&1 | grep -i "bitrate\|profile\|level" || true

if [ -f compare_topsenc.h264 ]; then
    TOPSENC_SIZE=$(stat -c%s compare_topsenc.h264)
    echo -e "${GREEN}✓ topsenc: ${TOPSENC_SIZE} bytes${NC}"
fi

# Test 3: Analysis
echo ""
echo -e "${YELLOW}[3/4] Analyzing streams...${NC}"

# Use mediainfo if available, otherwise basic info
if command -v mediainfo &> /dev/null; then
    echo "enriginenc stream info:"
    mediainfo compare_enrigin.h264 | grep -E "Bit rate|Width|Height|Frame rate|Profile|Level" || true
    echo ""
    echo "topsenc stream info:"
    mediainfo compare_topsenc.h264 | grep -E "Bit rate|Width|Height|Frame rate|Profile|Level" || true
else
    echo "Install mediainfo for detailed analysis: apt-get install mediainfo"
fi

# Test 4: Size comparison
echo ""
echo -e "${YELLOW}[4/4] Size Comparison:${NC}"
echo "  enriginenc: $ENRIGIN_SIZE bytes ($(echo "scale=2; $ENRIGIN_SIZE / 1024" | bc) KB)"
echo "  topsenc:    $TOPSENC_SIZE bytes ($(echo "scale=2; $TOPSENC_SIZE / 1024" | bc) KB)"

DIFF=$((ENRIGIN_SIZE - TOPSENC_SIZE))
PERCENT=$(echo "scale=1; $DIFF * 100.0 / $TOPSENC_SIZE" | bc)
echo "  Difference: $DIFF bytes (+${PERCENT}%)"
echo ""

# Calculate actual bitrate
DURATION=$(echo "scale=2; $FRAMES / 30.0" | bc)
ENRIGIN_BITRATE=$(echo "scale=0; $ENRIGIN_SIZE * 8 / $DURATION / 1000" | bc)
TOPSENC_BITRATE=$(echo "scale=0; $TOPSENC_SIZE * 8 / $DURATION / 1000" | bc)

echo -e "${YELLOW}Actual Bitrates:${NC}"
echo "  enriginenc: ${ENRIGIN_BITRATE} kbps (target: ${BITRATE} kbps)"
echo "  topsenc:    ${TOPSENC_BITRATE} kbps (target: ${BITRATE} kbps)"
echo ""

# Possible reasons
echo -e "${YELLOW}Possible reasons for size difference:${NC}"
echo "  1. Different GOP structure (enriginenc GOP=1 enforced)"
echo "  2. Different QP settings"
echo "  3. Different rate control implementation"
echo "  4. OTL may apply additional optimizations"
echo "  5. Different encoder preset interpretations"
echo ""

echo -e "${GREEN}=== Analysis Complete ===${NC}"
echo ""
echo "To visually compare quality:"
echo "  ffplay compare_enrigin.h264"
echo "  ffplay compare_topsenc.h264"
echo ""
echo "To decode and compare frame by frame:"
echo "  ffmpeg -i compare_enrigin.h264 -f rawvideo enrigin_%04d.yuv"
echo "  ffmpeg -i compare_topsenc.h264 -f rawvideo topsenc_%04d.yuv"
